<template>
  <div class="m-container" v-loading="planLoading">
    <div class="plan-list-box">
      <div class="query-box">
        <el-radio-group v-model="modeRadio">
          <el-radio-button v-for="(item, index) in modeData" :key="item" :label="item.label" @change="modeChange(index)"></el-radio-button>
        </el-radio-group>
      </div>
      <div class="content">
        <el-collapse v-if="planList.length && formGroup.searchType === 1" accordion>
          <el-collapse-item  v-for="item in planList" :key="item.id"  @click="getOneSubject(item.lessonId)">
            <template #title>
              {{ item.lessonName }}
            </template>
            <div v-for="sub in studentSubject" 
              :key="sub" class="student-subject" 
              :class="{ active: currentId === sub.contractId }" 
              @click="setCurrentId(sub.contractId), getClassTimesList(sub.contractId)">
              <span>{{ sub.contractName }}</span>
              <span>签约时间：{{ Moment(sub.createTime).format("YYYY-MM-DD")}}</span>
              <i class="el-icon-caret-right" />
            </div>
          </el-collapse-item>
        </el-collapse>
        <ul v-else-if="planList.length && formGroup.searchType !== 1">
          <li v-for="item in planList" :key="item.id" :class="{ active: currentId === item.id }" @click="setCurrentId(item.id), getClassTimesList(item.lessonId)">
            <span>{{ item.lessonName }}</span>
            <i class="el-icon-caret-right" />
          </li>
        </ul>
        <cus-empty v-else-if="!planList.length" />
      </div>
    </div>
    <div class="class-times-box" v-loading="classTimesLoading">
      <ul v-if="classTimesList.length">
        <div class="all-check"><el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox></div>
        <li v-for="item in classTimesList" :key="item" @click="checkChange(item)">
          <el-checkbox :modelValue="checkedList.includes(item)" />
          <span>{{ item.indexName }}</span>
        </li>
      </ul>
      <cus-empty v-else />
    </div>
  </div>
</template>

<script lang="ts">
import { ref, reactive } from 'vue'
import $ from '$';
import { ElMessage } from 'element-plus'
import Moment from 'moment'

export default {
  props: [ 'subjectId', 'excludId' ],
  setup(props) {
    let formGroup = reactive({
      searchType: 1,
      current: 1,
      size: 99,
    });
    let modeRadio = ref('一对一')
    let modeData = ref([{ label:'一对一' }, { label:'小组课' }])
    let queryMap = reactive({ grades: [], terms: [] });

    let [ planLoading, setPlanLoading] = $.useState(false);
    let [ planList, setPlanList ] = $.useState([]);
    const modeChange = async (idx) => {
      formGroup.searchType = idx + 1
      setPlanLoading(true);
      let res = await $.http.post('/admin/lessonInfo/queryStudentOrGroups', { ...formGroup});
      setPlanList(res.json.records);
      setPlanLoading(false);
    }
    modeChange(0);
    let studentSubject = ref()
    const getOneSubject = (studentId) => {
      $.http.post('/admin/studentInfo/queryContract', { studentId : studentId }).then(res => {   
        res.result ? studentSubject.value = res.json : ElMessage.error(res.msg)
      })
    }
    let [ classTimesLoading, setClassTimesLoading ] = $.useState(false);
    let [ classTimesList, setClassTimesList ] = $.useState([]);
    const getClassTimesList = async (lessonId) => {
      setClassTimesLoading(true);
      let res = await $.http.post('/admin/lessonTimes/queryContent', { lessonId });
      setClassTimesLoading(false);
      setCheckList([]);
      setClassTimesList(res.json);
    }

    let [ currentId, setCurrentId ] = $.useState(null);

    let [ checkedList, setCheckList ] = $.useState<any[]>([]);
    let isIndeterminate = ref(true)
    let checkAll = ref(false)

    const handleCheckAllChange = (val) =>  {
      checkedList.value = val ? classTimesList.value : [];
      isIndeterminate.value = false;
    }
    const checkChange = (data) => {
      let index = checkedList.value.indexOf(data);
      index > -1 ? checkedList.value.splice(index, 1) : checkedList.value.push(data);
    }

    const save = (resolve, reject) => {
      if (checkedList.value.length) {
        resolve([checkedList.value, currentId.value])
      } else {
        reject(); ElMessage.warning('请选择教学计划！')
      }
    };
 
    return { 
      formGroup, modeChange, queryMap, planLoading, planList, classTimesLoading, getClassTimesList, classTimesList, currentId, setCurrentId, checkedList, checkChange, save,
      modeRadio, modeData, isIndeterminate, checkAll, handleCheckAllChange, getOneSubject, studentSubject, Moment }
  }
}
</script>

<style lang="scss" scoped>
.m-container {
  display: flex;
  .plan-list-box {
    display: flex;
    flex-direction: column;
    width: 300px;
    .query-box {
      height: 36px + 16px * 2;
      padding: 16px 24px;
      :deep(.el-input) {
        width: 100px;
        margin-right: 10px;
      }
    }
    .content {
      flex: 1 1 78px;
      min-height: 300px;
      overflow: auto;
      li {
        height: 48px;
        line-height: 48px;
        display: flex;
        padding: 0 15px 0 30px;
        color: #333;
        border-bottom: solid 1px #DCDEE3;
        cursor: pointer;
        &.active {
          color: #72b22a;
          background: #F6F7F9;
          pointer-events: none;
          i {
            display: block;
          }
        }
        &:hover {
          background: #F6F7F9;
        }
        i {
          display: none;
          margin-left: auto;
          color: #77808D;
          line-height: 48px;
        }
      }
    }
  }
  .class-times-box {
    width: 190px;
    padding: 15px 20px;
    background: #F6F7F9;
    ul {
      overflow: auto;
    }
    li {
      cursor: pointer;
      overflow: hidden;
      white-space: nowrap;
      :deep(.el-checkbox) {
        margin-right: 8px;
        pointer-events: none;
      }
      &:not(:last-child) {
        margin-bottom: 18px;
      }
    }
  }
  .all-check {
    margin-bottom: 16px;
  }
  .student-subject {
    margin-left: 40px;
    line-height: 30px;
    height: 30px;
    cursor: pointer;
    display: flex;
    & > span:nth-of-type(2) {
      margin-left: 10px;
    }
    i {
      display: none;
      margin-left: auto;
      color: #77808D;
      line-height: 30px;
      margin-right: 20px;
    }
    &.active {
      color: #72b22a;
      background: #F6F7F9;
      pointer-events: none;
      i {
        display: block;
      }
    }
    &:hover {
      background: #F6F7F9;
    }
  }
}
:deep(.el-collapse-item__header) {
  padding-left: 30px;
  font-size: 14px;
}
:deep(.el-collapse) {
  border-top: none;
}
</style>